package Shapes;

import processing.core.PApplet;
import processing.core.PImage;

public class Ball extends shape {
	
	
	String process_type = "" ;
	
    int num_process = 7;
    int num_impacts = 2;
    

	// Screen values
	public float xpos, ypos;
	public float tempxpos, tempypos;
	int size = 20;
	boolean over = false;
	boolean move = false;

	// Spring simulation constants
	float mass = 10.0f; // Mass
	float k = 0.1f; // Spring constant
	float damp = 0.9f; // Damping
	float rest_posx; // Rest position X
	float rest_posy; // Rest position Y

	// Spring simulation variables
	// float pos = 20.0; // Position
	float velx = 0.0f; // X Velocity
	float vely = 0.0f; // Y Velocity
	float accel = 0; // Acceleration
	float force = 0; // Force

	String name = "";
	int index;
//	int type; // 1- process 2-impact, we don't need it any more, because we seperated them
	public boolean isShowCheckbox = false;
	
	int season = 10;

	
	PImage[] processsprite = new PImage[num_process]; // the pic for the process_sprite
	PImage[] impactsprite = new PImage[num_impacts]; // the pic for the process_sprite
	
	float end = 0;

	//button_process[i] = new (width_process_interval * (i + 1), 30, 50, 0.9f,10.0f, 0.1f, button_process, i, name, 1);
	
	
	public Ball(PApplet p) {
		super(p);
		// TODO Auto-generated constructor stub
	}
	
	
	  public void setup(String type, int x, int y, int s, int index, int period) {
		   
		  this.xpos = this.tempxpos = x;
			this.ypos = this.tempypos = y;
			this.rest_posx = x;
			this.rest_posy = y;
			this.size = s;
	        this.index = index;
			this.name = type; 
		    this.season = period;
			// ----------------------------------------------------------//
			// ----------------------------here we give six kind of
			// pictures------------------------------//		
			processsprite[0] = parent.loadImage("../resource/farming.gif");
			processsprite[1] = parent.loadImage("../resource/manufacturing.gif");
			processsprite[2] = parent.loadImage("../resource/transportation.gif");
			processsprite[3] = parent.loadImage("../resource/retail.gif");
			processsprite[4] = parent.loadImage("../resource/storage.gif");
			processsprite[5] = parent.loadImage("../resource/cooking.gif");
			processsprite[6] = parent.loadImage("../resource/cooking.gif");


			impactsprite[0] = parent.loadImage("../resource/storage.gif");
			impactsprite[1] = parent.loadImage("../resource/cooking.gif");
			
		  }
	
//	  public void draw(){
//		 
//		  parent.fill(parent.color(0,255,0));
//		  parent.ellipse(x, y, radius, radius);
//		  parent.fill(255);
//		//  ellipse(this.width / 2, this.height / 2, impact_width, impact_height);
//		//  createGradient(x, y, radius, parent.color(0,255,0), parent.color(0, 0, 0));
//	  }
	  

		public void draw() {
			
			drawcycle();
			if (over) {

				parent.fill(255);
				parent.text(name, 850, 300);

				parent.fill(153);

			} else {
				parent.fill(255);
			}
			// ellipse(tempxpos, tempypos, size, size);
			int picindex = -1;

			
				if (name.equalsIgnoreCase("farming")) {
					picindex = 0;
				} else if (name.equalsIgnoreCase("manufacturing")) {
					picindex = 1;
				} else if (name.equalsIgnoreCase("transportation")) {
					picindex = 2;
				} else if (name.equalsIgnoreCase("retail")) {
					picindex = 3;
				} else if (name.equalsIgnoreCase("storage")) {
					picindex = 4;
				} else if (name.equalsIgnoreCase("cooking")) {
					picindex = 5;
				}else if(name.equalsIgnoreCase("wastecycling")){
					picindex = 6;
				}

				parent.image(processsprite[picindex], tempxpos - 30, tempypos - 30,
						60, 60);
				
				
			} 

	  private void drawcycle() {
	// TODO Auto-generated method stub
		  if(end == parent.PI*2)
		  {
			  end = 0;
		  }
		  end += parent.PI*2/this.season;
		  parent.fill(0,250,250,191);
		  parent.arc(tempxpos, tempypos, 80, 80, 0, end);
}


	public void createGradient (float x, float y, float radius, int c1, int c2){
		  float px = 0, py = 0, angle = 0;

		  // calculate differences between color components 
		  float deltaR = parent.red(c2)-parent.red(c1);
		  float deltaG = parent.green(c2)-parent.green(c1);
		  float deltaB = parent.blue(c2)-parent.blue(c1);
		  // hack to ensure there are no holes in gradient
		  // needs to be increased, as radius increases
		  float gapFiller = 1.0f;

		  for (int i=0; i< radius; i++){
		    for (float j=0; j<360; j+=1.0f/gapFiller){
		      px = x+parent.cos(parent.radians(angle))*i;
		      py = y+parent.sin(parent.radians(angle))*i;
		      angle+=1.0f/gapFiller;
		      int c = parent.color(
		      (parent.red(c1)+(i)*(deltaR/radius)),
		      (parent.green(c1)+(i)*(deltaG/radius)),
		      (parent.blue(c1)+(i)*(deltaB/radius)) 
		        );
		      parent.set(PApplet.parseInt(px), PApplet.parseInt(py), c);      
		    }
		  }
		  // adds smooth edge 
		  // hack anti-aliasing
		  parent.noFill();
		  parent.strokeWeight(3);
		  parent.ellipse(x, y, radius*2, radius*2);
		}
	  
	  
	 
	  
	  public void updatePos(float posx, float posy, float posz){
		  this.x = posx;
		  this.y = posy;
		  this.z = posz;
	  }
	  
			public void update() {
				if (move) {
					rest_posy = parent.mouseY;
					rest_posx = parent.mouseX;
				}

				force = -k * (tempypos - rest_posy); // f=-ky
				accel = force / mass; // Set the acceleration, f=ma == a=f/m
				vely = damp * (vely + accel); // Set the velocity
				tempypos = tempypos + vely; // Updated position

				force = -k * (tempxpos - rest_posx); // f=-ky
				accel = force / mass; // Set the acceleration, f=ma == a=f/m
				velx = damp * (velx + accel); // Set the velocity
				tempxpos = tempxpos + velx; // Updated position

		//		if ((over() || move) && !otherOver()) {
				if ((over() || move)) {
					over = true;
				} else {
					over = false;
				}
			}

			// Test to see if mouse is over this spring
			public boolean over() {
				float disX = tempxpos - parent.mouseX;
				float disY = tempypos - parent.mouseY;
				if (parent.sqrt(parent.sq(disX) + parent.sq(disY)) < size) {
					return true;
				} else {
					return false;
				}
			}

			public void pressed() {
				if (over) {
					move = true;
				} else {
					move = false;
				}
			}

			public void released() {
				move = false;
				rest_posx = xpos;
				rest_posy = ypos;

			}

			public boolean isPressed() {
				if (over) {
					return true;
				} else {
					return false;
				}
			}

			public void mousePassby() {
				if (over()) {
					this.isShowCheckbox = true;
				} else {
					this.isShowCheckbox = false;
				}
				
			}
		
		
}
